package com.szholly.pro.watergas_server.ps.android.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.ServletContextAware;

import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.pro.watergas_server.common.data.ParameterSqlModel;
import com.szholly.pro.watergas_server.common.util.JsonUtil;
import com.szholly.pro.watergas_server.common.util.RedisHelper;
import com.szholly.pro.watergas_server.ps.android.common.ResultUtils;
import com.szholly.pro.watergas_server.ps.android.service.MoveDataService;
import com.szholly.pro.watergas_server.ps.service.MapInfoService;
import com.szholly.pro.watergas_server.ps.service.TwoTuple;
import com.szholly.data.general.GlobalParameter;
import com.szholly.data.general.RowBase;

import lrq.common.StringUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/android/wdp/*")
public class AndroidForWdp implements ServletContextAware {
	// Spring这里是通过实现ServletContextAware接口来注入ServletContext对象
	@SuppressWarnings("unused")
	private ServletContext servletContext;
	@Autowired
	MapInfoService MapInfoService;
	private PipelinesPagingManager pipelinesManager;
	private PipelinesPointManager pipelinesPointManager;

	@Override
	public void setServletContext(ServletContext arg0) {
		this.servletContext = arg0;
	}

	@RequestMapping("/getSplashInitData")
	@ResponseBody
	public String getSplashInitData() {
		try {
			List<RowBase> factoryList = MoveDataService.getFactoryList(null);
			List<RowBase> stationList = MoveDataService.getStationList(null,null);
			List<RowBase> parameterList = MoveDataService.getParameterList();
			List<RowBase> administrativeDivisionList = MoveDataService.getAdministrativeDivisionList();
			PipeService wdpService = PipeService.getInstance();
			
			
			Map<String, Object> resultMap = new HashMap<>();
			resultMap.put("factoryList", factoryList);
			resultMap.put("stationList", stationList);
			resultMap.put("parameterList", parameterList);
			resultMap.put("administrativeDivisionList", administrativeDivisionList);
			resultMap.put("projectList", wdpService.getProjectList());
			resultMap.put("projectItemList", wdpService.getProjectItemList());
			resultMap.put("pipeList", wdpService.getPipeList());
			return ResultUtils.getToastResult(resultMap);
		} catch (Exception e) {
			return ResultUtils.getToastResult(e);
		}
	}

	// 内窥检测 1.获取属性复核数据
	@RequestMapping("/getEndoscopicReview")
	@ResponseBody
	public String getEndoscopicReview(String pipeId) {
		JSONObject resultObject = new JSONObject();
		// 如果为空的默认信息
		String defaultString = "(未查到相关信息)";
		String error = "";
		JSONArray resultArray = new JSONArray();
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		parms.add(new GlobalParameter("pipeId", pipeId));
		String sql = "SELECT ATTR_REVIEW_ID,NO,FLOW,PIPE_TYPE,PIPE_MATERIAL,PIPE_DIAMETER,SURVEYOR,WELL_NO_REVIEW" + ",WELL_NO_DESIGN,t2.S_DOMAINCAPTION PIPE_MATERIAL_REVIEW,t1.S_DOMAINCAPTION PIPE_MATERIAL_DESIGN" + ",PIPE_DIAMETER_VIEW,PIPE_DIAMETER_DESIGN,SECTION_SIZE_REVIEW,SECTION_SIZE_DESIGN,X_REVIEW,Y_REVIEW" + ",X_DESIGN,Y_DESIGN,PILE_TOP_REVIEW,PILE_TOP_DESIGN,PLANE_DIFF,HEIGHT_DIFF,ATTR_REVIEW_ADDRESS" + ",TO_CHAR(DETECTION_DATE,'yyyy:mm:dd hh24:mi:ss') as DETECTION_DATE from attr_review t" + " LEFT JOIN (select S_DOMAINCODE,S_DOMAINCAPTION from DATA_DEFULATDOMAIN where S_DOMAINNAME = 'PIPE_MATERIAL') t1" + " on t1.S_DOMAINCODE = t.PIPE_MATERIAL_DESIGN" + " LEFT JOIN (select S_DOMAINCODE,S_DOMAINCAPTION from DATA_DEFULATDOMAIN where S_DOMAINNAME = 'PIPE_MATERIAL') t2" + " on t2.S_DOMAINCODE = PIPE_MATERIAL_REVIEW where 1 = 1";
		if (pipeId != null && !pipeId.isEmpty()) {
			sql += " and PIPE_NO = @pipeId";
		}
		List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
		JSONObject beanObject;
		JSONArray beanArray;
		JSONObject beanResultObject;
		if (rows.size() > 0) {
			for (RowBase bean : rows) {
				beanArray = new JSONArray();
				beanObject = new JSONObject();
				beanObject.put("designValue", toNotNullString(bean.getValue("WELL_NO_DESIGN")));
				beanObject.put("reviewValue", toNotNullString(bean.getValue("WELL_NO_REVIEW")));
				beanObject.put("valueName", "点号");
				beanArray.add(beanObject);
				beanObject = new JSONObject();
				beanObject.put("designValue", toNotNullString(bean.getValue("PIPE_MATERIAL_DESIGN")));
				beanObject.put("reviewValue", toNotNullString(bean.getValue("PIPE_MATERIAL_REVIEW")));
				beanObject.put("valueName", "材质");
				beanArray.add(beanObject);
				beanObject = new JSONObject();
				beanObject.put("designValue", toNotNullString(bean.getValue("PIPE_DIAMETER_DESIGN")));
				beanObject.put("reviewValue", toNotNullString(bean.getValue("PIPE_DIAMETER_VIEW")));
				beanObject.put("valueName", "管径");
				beanArray.add(beanObject);
				beanObject = new JSONObject();
				beanObject.put("designValue", toNotNullString(bean.getValue("SECTION_SIZE_DESIGN")));
				beanObject.put("reviewValue", toNotNullString(bean.getValue("SECTION_SIZE_REVIEW")));
				beanObject.put("valueName", "断面尺寸");
				beanArray.add(beanObject);
				beanObject = new JSONObject();
				beanObject.put("designValue", toNotNullString(bean.getValue("X_DESIGN")));
				beanObject.put("reviewValue", toNotNullString(bean.getValue("X_REVIEW")));
				beanObject.put("valueName", "X");
				beanArray.add(beanObject);
				beanObject = new JSONObject();
				beanObject.put("designValue", toNotNullString(bean.getValue("Y_DESIGN")));
				beanObject.put("reviewValue", toNotNullString(bean.getValue("Y_REVIEW")));
				beanObject.put("valueName", "Y");
				beanArray.add(beanObject);
				beanObject = new JSONObject();
				beanObject.put("designValue", toNotNullString(bean.getValue("PILE_TOP_DESIGN")));
				beanObject.put("reviewValue", toNotNullString(bean.getValue("PILE_TOP_REVIEW")));
				beanObject.put("valueName", "管底标高");
				beanArray.add(beanObject);
				beanResultObject = new JSONObject();
				beanResultObject.put("valueList", beanArray);
				beanResultObject.put("id", toNotNullString(bean.getValue("NO")));
				beanResultObject.put("pipeNo", toNotNullString(bean.getValue("PIPE_NO")));
				beanResultObject.put("flow", toNotNullString(bean.getValue("FLOW")));
				beanResultObject.put("pipeType", toNotNullString(bean.getValue("PIPE_TYPE")));
				beanResultObject.put("surveyor", toNotNullString(bean.getValue("SURVEYOR")));
				beanResultObject.put("dateTime", toNotNullString(bean.getValue("DETECTION_DATE")));
				beanResultObject.put("planeDiff", toNotNullString(bean.getValue("PLANE_DIFF")));
				beanResultObject.put("heightDiff", toNotNullString(bean.getValue("HEIGHT_DIFF")));
				beanResultObject.put("address", toNotNullString(bean.getValue("ATTR_REVIEW_ADDRESS")));
				resultArray.add(beanResultObject);
			}
		}
		resultObject.put("error", error);
		resultObject.put("defaultString", defaultString);
		resultObject.put("data", resultArray);
		return resultObject.toString();
	}

	// 内窥检测 获取项目列表，工程列表，管道列表，分区列表
	@RequestMapping("/getEndoscopicInit")
	@ResponseBody
	public String getEndoscopicInit() {
		JSONObject resultJson = new JSONObject();
		JSONArray projectArray = new JSONArray();
		JSONArray projectItemArray = new JSONArray();
		JSONArray pipeArray = new JSONArray();
		JSONArray unitArray = new JSONArray();
		String projectSql = "select PROJECT_NO \"id\",PROJECT_NAME \"name\" ,DETECTION_COMPANY \"company\" from PROJECT";
		String pipeSql = "SELECT PIPE_NO \"id\",PIPE_SECTION_NAME \"name\",PROJECT_ITEM_NO \"fatherId\" FROM PIPE_SECTION ORDER BY PROJECT_ITEM_NO ASC";
		String projectItemSql = "select ITEM_NO \"id\",PROJECT_NO \"fatherId\",ITEM_NAME \"name\",NVL(t2.S_DOMAINCAPTION, '未知分区') as \"unit\",DETECTION_LENGTH \"length\"" + " from project_item t1" + " left join(select s_id,S_DOMAINCAPTION from DATA_DEFULATDOMAIN where S_DOMAINNAME = '政区' and length(s_id)='2') t2 on t2.s_id =t1.DISTRICT where 1 = 1";
		String unitSql = "select S_ID \"id\",S_DOMAINCAPTION \"name\" from DATA_DEFULATDOMAIN where S_DOMAINNAME = '政区' and length(s_id)='2' and S_DOMAINCAPTION <> '前海管理局'";
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		List<RowBase> rows = DataUtil.getSelectRows(projectSql, parms);
		if (rows.size() > 0) {
			projectArray = JSONArray.fromObject(rows);
		}
		rows = DataUtil.getSelectRows(projectItemSql, parms);
		if (rows.size() > 0) {
			projectItemArray = JSONArray.fromObject(rows);
		}
		rows = DataUtil.getSelectRows(pipeSql, parms);
		if (rows.size() > 0) {
			pipeArray = JSONArray.fromObject(rows);
		}
		rows = DataUtil.getSelectRows(unitSql, parms);
		if (rows.size() > 0) {
			unitArray = JSONArray.fromObject(rows);
		}
		resultJson.put("projectList", projectArray);
		resultJson.put("projectItemList", projectItemArray);
		resultJson.put("pipeList", pipeArray);
		resultJson.put("unitList", unitArray);
		return resultJson.toString();
	}

	// 内窥检测 三个参数筛选，优先级由低到高projectId projectItemId pipeId
	@RequestMapping("/getEndoscopicDefect")
	@ResponseBody
	public String getEndoscopicDefect(String projectId, String projectItemId, String pipeId, String defectId) {
		// 默认数据
		JSONObject resultJson = new JSONObject();
		JSONArray defectArray = new JSONArray();
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		String defectSql = "select t.LNO \"id\",t.PROJECT_ITEM_NO \"projectItemNo\",t.PIPE_NO \"pipeNo\" ,t.FLOW \"flow\"" + ",t.PIPE_TYPE \"pipeType\",t.PIPE_DIAMETER \"pipeDiameter\""

				+ ",NVL(t1.NAME,t.PIPE_MATERIAL) \"pipeMaterial\",t.SURVEYOR \"surveyor\""

				+ ",TO_CHAR(t.DETECTION_DATE,'hh-mm-dd hh24:mi:ss') as \"dateTime\",t.START_WELL_NO \"startWellNo\"" + ",t.END_WELL_NO \"endWellNo\",t.DIRECTION \"direction\",NVL(t5.S_DOMAINCAPTION,t.DEFECT_TYPE) \"defectType\"" + ",t.DEFECT_LEVEL \"defectLevel\",t2.S_DOMAINCAPTION \"defectLevelDes\",t.RI \"ri\""

				+ ",NVL(t3.S_DOMAINCAPTION,t.RI_LEVEL) \"riLevel\""

				+ ",t3.S_DOMAINCAPTION \"riLevelDes\",t.RI_SUGGESS \"riSuggest\",t.MI \"mi\",t.MI_LEVEL \"miLevel\""

				+ ",NVL(t4.S_DOMAINCAPTION,t.MI_LEVEL) \"miLevelDes\",t.MI_SUGGESS \"miSuggest\",t.LENGTH \"length\",t.PHOTO \"photoUrl\""

				+ ",t.VIDEO \"videoUrl\" from NKJC_DEFECT t" + " LEFT JOIN PIPE_MATERIAL t1 on t1.PIPE_MATERIAL_ID = t.PIPE_MATERIAL" + " LEFT JOIN (select * from DATA_DEFULATDOMAIN where S_DOMAINNAME='DEFECT_LEVEL')t2 ON t2.S_DOMAINCODE = t.DEFECT_LEVEL" + " LEFT JOIN (select * from DATA_DEFULATDOMAIN  where S_DOMAINNAME='RI_LEVEL')t3 ON t3.S_DOMAINCODE = t.RI_LEVEL" + " LEFT JOIN (select * from DATA_DEFULATDOMAIN  where S_DOMAINNAME='MI_LEVEL')t4 ON t4.S_DOMAINCODE = t.MI_LEVEL" + " LEFT JOIN (select * from DATA_DEFULATDOMAIN  where S_DOMAINNAME='DEFECT_TYPE')t5 ON t5.S_DOMAINCODE = t.DEFECT_TYPE OR t5.S_DOMAINCAPTION = t.DEFECT_TYPE" + " LEFT JOIN PROJECT_ITEM t6 ON t6.ITEM_NO = t.PROJECT_ITEM_NO" + " where 1 = 1";
		if (defectId != null && !defectId.isEmpty()) {
			parms.add(new GlobalParameter("defectId", defectId));
			defectSql += " and t.LNO = @defectId";
		} else if (pipeId != null && !pipeId.isEmpty()) {
			parms.add(new GlobalParameter("pipeId", pipeId));
			defectSql += " and t.PIPE_NO = @pipeId";
		} else if (projectItemId != null && !projectItemId.isEmpty()) {
			parms.add(new GlobalParameter("projectItemId", projectItemId));
			defectSql += " and t.PROJECT_ITEM_NO = @projectItemId";
		} else if (projectId != null && !projectId.isEmpty()) {
			parms.add(new GlobalParameter("projectId", projectId));
			defectSql += " and t6.PROJECT_NO = @projectId";
		} else {
			// 没有参数不让查，因为数据量太多
			resultJson.put("message", "参数异常");
			resultJson.put("resultCode", "0");
			resultJson.put("defectList", defectArray);
			return resultJson.toString();
		}
		try {
			defectSql += " ORDER BY t.PROJECT_ITEM_NO , t.PIPE_NO";
			List<RowBase> rows = DataUtil.getSelectRows(defectSql, parms);
			defectArray = JSONArray.fromObject(rows);
			resultJson.put("message", "成功");
			resultJson.put("resultCode", "1");
			resultJson.put("defectList", defectArray);
		} catch (Exception e) {
			e.printStackTrace();
			resultJson.put("resultCode", "0");
			resultJson.put("message", "SQL查询异常");
			resultJson.put("defectList", defectArray);
		}
		return resultJson.toString();
	}

	// 排水管网 地图上点的查询
	@RequestMapping("/getPipelinesPointData")
	@ResponseBody
	public String getPipelinesPointData(String type, String params) {
		if (pipelinesPointManager == null) {
			pipelinesPointManager = new PipelinesPointManager();
		}
		return pipelinesPointManager.getMapPointData(type, params);
	}

	// 排水管网列表分页查询查询
	@RequestMapping("/getPipelinesPagingData")
	@ResponseBody
	public String getPipelinesPagingData(String type, String params) {
		if (pipelinesManager == null) {
			pipelinesManager = new PipelinesPagingManager();
		}
		return pipelinesManager.getPipelinesPagingData(type, params);
	}

	// 属性复核详情分页查询
	@RequestMapping("/getEndoscopicReviewDetail")
	@ResponseBody
	public String getEndoscopicReviewDetail(String pipeCode, String pageIndex) {
		try {
			int pageIndexInt = Integer.parseInt(pageIndex);
			PipeService service = PipeService.INSTANCE;
			RowBase reviewDetailRowBase = service.getEndoscopicReviewDetail(pipeCode, pageIndexInt);
			return ResultUtils.getToastResult(reviewDetailRowBase);
		} catch (Exception e) {
			return ResultUtils.getToastResult(e);
		}
	}

	// 获取偏差精度统计列表-如果id为空，则获取全部数据
	@RequestMapping("/getEndoscopicPrecision")
	@ResponseBody
	public String getEndoscopicPrecision(String projectCode) {
		try {
			PipeService service = PipeService.INSTANCE;
			List<RowBase> list = service.getEndoscopicPrecisionList(projectCode);
			return ResultUtils.getToastResult(list);
		} catch (Exception e) {
			return ResultUtils.getToastResult(e);
		}
	}

	@RequestMapping("/getArcInitData")
	@ResponseBody
	public String getArcInitData() {
		try {
			PipeService service = PipeService.INSTANCE;
			List<RowBase> streetList = service.getStreetList();
			List<RowBase> unitList = service.getUnitList();
			List<RowBase> typeList = service.getTypeList();
			Map<String, Object> map = new HashMap<>();
			map.put("unitList", unitList);
			map.put("streetList", streetList);
			map.put("typeList", typeList);
			return ResultUtils.getToastResult(map);
		} catch (Exception e) {
			return ResultUtils.getToastResult(e);
		}
	}

	private Object toNotNullString(Object text) {
		if (text == null) {
			return "";
		} else {
			return text.toString();
		}
	}

	/**
	 * *************************************************************************
	 * *********************************************************
	 */

	/**
	 * 获取查询数据
	 * 
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getDistrict")
	@ResponseBody
	public String getDistrict() throws Exception {

		TwoTuple<List<RowBase>, HashMap<String, List<RowBase>>> result = MapInfoService.getDistrict();

		JSONArray quJsonObject = JsonUtil.ConvertArrayToJsonObject(result.first);
		JSONObject jdJsobObject = JsonUtil.ConvertObjectToJsonObject(result.second);
		JSONObject resultObject = new JSONObject();
		resultObject.put("qu", quJsonObject);
		resultObject.put("jd", jdJsobObject);
		String jsonResult = JsonUtil.ConvertObjectToJson(resultObject);
		// BaseController.outString(jsonResult);
		return jsonResult;
	}

	/**
	 * 查询管网点
	 * 
	 * @param parms
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/pipePoint")
	@ResponseBody
	public String getPipePoint(String parms) throws Exception {
		HashMap<String, String> parms1 = new HashMap();
		try {
			parms1 = getMapData(parms);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String sql = "select '立岗西路一号点' as ADDRESS,'立岗西路一号点' as name,t1.photo,t2.shape.minx x,t2.shape.miny y,t2.OBJECTID,t1.exp_no station_code, " + " sde.st_astext(t2.shape) wkt " + "from BC_SITE_INFO t1\n" + "left join  sde.monitor_station t2 on t1.exp_no=t2.exp_no\n" + "where t1.exp_no='WS090100500105' ";
		String result = DataUtil.getPageSelectJson(sql, null);
		return result;
	}

	// 获取排水管网信息
	@RequestMapping("/getPSGW")
	@ResponseBody
	public String getGWInfo(String parmas) {
		// JSONObject aa = JSONObject.fromObject(parmas);
		HashMap<String, String> parms = new HashMap<String, String>();
		try {
			parms = getMapData(parmas);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String sql = " SELECT  t.行政区划 || t.街道名称 address," + " '材料：' || t.管线材料  || ' 管径：' || t.管径或断面 || ' 类型：' || t.排水 tip," + "t.shape.minx x," + "t.shape.miny y," + "t.OBJECTID, " + "t.街道名称 || ' ' || t.本点号 name," + "t.本点号," + "t.管线材料," + "t.管径或断面," + "t.S_POINT," + "t.E_POINT," + "t.路名," + "t.街道名称," + " sde.st_astext(t.shape) wkt," + "t.行政区划," + "t.排水 管网类型," + "t.CONSTER_DEPT 建设单位 ,t.PIPELENGTH " + "FROM sde.ps_pipe t where 1=1 ";

		ParameterSqlModel parmSql = processParamSql(parms, sql);
		List<GlobalParameter> params = parmSql.getParms();
		sql = parmSql.getSql();

		if (parms.containsKey("keyWord")) {
			String keyWord = parms.get("keyWord");
			sql += " and (t.街道名称 like '%" + keyWord + "%' or t.行政区划 like '%" + keyWord + "%' or t.管线材料 like '%" + keyWord + "%' or t.管径或断面 like '%" + keyWord + "%' or t.排水 like '%" + keyWord + "%')";
		}
		String street = parms.get("STREET");
		if (!StringUtil.isEmpty(street)) {
			sql += " and t.街道名称 = @street";
			params.add(new GlobalParameter("street", street));
		}
		String type = parms.get("TYPE");
		if (!StringUtil.isEmpty(type)) {
			sql += " and t.管网类型 = @type";
			params.add(new GlobalParameter("type", type));
		}
		String result = null;
		try {
			result = DataUtil.getPageSelectJson(sql, params);
			List<RowBase> result1 = DataUtil.getPageList(sql, null, null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	private ParameterSqlModel processParamSql(HashMap<String, String> parms, String sql) {

		List<GlobalParameter> parmsList = new ArrayList<>();

		if (parms.containsKey("districts")) {
			String[] districts = parms.get("districts").split(",");
			List<GlobalParameter> districtsParm = DataUtil.arrayToParameters(districts, "district");
			String districtInWhere = StringUtil.getArrayStrForSql(districts, "district");
			parmsList.addAll(districtsParm);
			sql += " and t.district in " + districtInWhere;
		}

		if (parms.containsKey("filter") && parms.containsKey("filterField")) {
			String filterField = parms.get("filterField");
			String filter = parms.get("filter");

			if (filter.indexOf(":null") > -1) {
				sql += " and t." + filterField + " is null ";
			} else {
				String[] filters = parms.get("filter").split(",");
				List<GlobalParameter> filterParm = DataUtil.arrayToParameters(filters, "filter");
				String filterInWhere = StringUtil.getArrayStrForSql(filters, "filter");
				parmsList.addAll(filterParm);
				sql += " and t." + filterField + " in " + filterInWhere;
			}
		}

		if (parms.containsKey("xmin") && parms.containsKey("xmax") && parms.containsKey("ymin") && parms.containsKey("ymax")) {
			String xmin = parms.get("xmin");
			String xmax = parms.get("xmax");
			String ymin = parms.get("ymin");
			String ymax = parms.get("ymax");

			if (sql.indexOf("monitor_station") > -1) {
				sql += " and t.x > @xmin " + " and x < @xmax " + " and y > @ymin " + " and y < @ymax ";
			} else {
				sql += " and t.shape.minx > @xmin " + " and t.shape.maxx < @xmax " + " and t.shape.miny > @ymin " + " and t.shape.maxy < @ymax ";
			}

			parmsList.add(new GlobalParameter("xmin", xmin));
			parmsList.add(new GlobalParameter("xmax", xmax));
			parmsList.add(new GlobalParameter("ymin", ymin));
			parmsList.add(new GlobalParameter("ymax", ymax));
		}

		ParameterSqlModel parmSql = new ParameterSqlModel();
		parmSql.setSql(sql);
		parmSql.setParms(parmsList);

		return parmSql;
	}

	/**
	 * 获取站点报警状态
	 * 
	 * @param id
	 * @return JSONObject warnDes(String)报警描述 code(String) "0"：表示无报警
	 */
	@RequestMapping("/getWarnState")
	@ResponseBody
	public String getWarnState(String id) {
		JSONObject resultJson = new JSONObject();
		resultJson.put("warnDes", "无报警");
		resultJson.put("code", "0");
		if (id != null && !id.isEmpty()) {
			List<GlobalParameter> parms = new ArrayList();
			parms.add(new GlobalParameter("id", id));
			String sql = "select DISTINCT t.type,t3.S_DOMAINCAPTION from OP_ALAR_INFO t" + " left join (select S_DOMAINCODE,S_DOMAINCAPTION from data_defulatdomain where S_DOMAINNAME='报警类型')t3" + " on t3.S_DOMAINCODE = t.type where t.STATION_CODE = @id";
			List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
			if (rows != null && rows.size() >= 0) {
				String alarmResult = "";
				for (RowBase rowBase : rows) {
					String alarmType = rowBase.getSafeString("S_DOMAINCAPTION");
					if (!alarmType.isEmpty()) {
						alarmResult = alarmResult + alarmType + " ";
					}
				}
				if (!alarmResult.isEmpty()) {
					resultJson.put("warnDes", alarmResult);
					resultJson.put("code", "1");
				}
			}
		}

		return resultJson.toString();
	}

	/**
	 * 获取清源小区数据
	 * 
	 * @param parms
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/getQYXQ")
	@ResponseBody
	public String getQYXQInfo(String parmas) throws Exception {
		HashMap<String, String> parms = new HashMap();
		try {
			parms = getMapData(parmas);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String sql = " SELECT  t.小区名称 name, t.位置 address ," + " '小区编号：' || t.小区编号  || ' 小区类型：' || t.小区类型 || ' 状态：' || t.状态 tip," + "t.shape.minx x," + "t.shape.miny y," + " sde.st_astext(t.shape) wkt," + "t.OBJECTID " + "FROM sde.building t where 1=1 ";

		ParameterSqlModel parmSql = processParamSql(parms, sql);
		List<GlobalParameter> params = parmSql.getParms();
		sql = parmSql.getSql();

		if (parms.containsKey("keyWord")) {
			String keyWord = parms.get("keyWord");
			sql += " and (t.小区编号 like '%" + keyWord + "%' or t.小区类型 like '%" + keyWord + "%' or t.状态 like '%" + keyWord + "%')";
		}

		String result = DataUtil.getPageSelectJson(sql, params);
		return result;
	}

	/**
	 * 获取规划管网数据
	 * 
	 * @param parms
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/getGHGW")
	@ResponseBody
	public String getGHGWInfo(String parmas) throws Exception {
		HashMap<String, String> parms = new HashMap();
		try {
			parms = getMapData(parmas);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String sql = "select  t.ALLNAME address, t.shape.minx x, " + "t.shape.miny y, " + " sde.st_astext(t.shape) wkt," + "t.objectid, " + "t.jdname || ' ' || t.objectid name " + " from sde.GHGW t where 1=1 ";

		ParameterSqlModel parmSql = processParamSql(parms, sql);
		List<GlobalParameter> params = parmSql.getParms();
		sql = parmSql.getSql();

		if (parms.containsKey("keyWord")) {
			String keyWord = parms.get("keyWord");
			sql += " and (t.objectid like '%" + keyWord + "%' )";
		}

		String result = DataUtil.getPageSelectJson(sql, params);
		return result;
	}

	/**
	 * 获取管网点数据
	 * 
	 * @param parms
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/getGWD")
	@ResponseBody
	public String getGWDInfo(String parmas) throws Exception {
		HashMap<String, String> parms = new HashMap();
		try {
			parms = getMapData(parmas);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String sql = "select '深圳市龙岗区金牛东路茜坑路段' as ADDRESS,'污水管网试点' as name,t1.photo,t2.shape.minx x,t2.shape.miny y,t2.OBJECTID,t1.exp_no station_code, " + " sde.st_astext(t2.shape) wkt " + "from BC_SITE_INFO t1\n" + "left join  sde.monitor_station t2 on t1.exp_no=t2.exp_no\n" + "where t1.exp_no='WS050400500268' ";

		/*
		 * ParameterSqlModel parmSql = processParamSql(parms, sql);
		 * List<GlobalParameter> params = parmSql.getParms(); sql =
		 * parmSql.getSql();
		 * 
		 * if(parms.containsKey("keyWord")){ String keyWord =
		 * parms.get("keyWord"); //sql += " and (t.POINT_TYPE like '%"+keyWord+
		 * "%' or t.附属物  like '%"+keyWord+"%' or t.路名  like '%"+keyWord+"%')"; }
		 */

		String result = DataUtil.getPageSelectJson(sql, null);
		return result;
	}

	/**
	 * 获取内窥检测管网数据
	 * 
	 * @param parms
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/getNKJCGW")
	@ResponseBody
	public String getNKJCGWInfo(String parmas) throws Exception {
		HashMap<String, String> parms = new HashMap();
		try {
			parms = getMapData(parmas);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String sql = "select * from( select  t.lno,t.ename name,t.shape,t1.defect_level,t4.project_dept  address,\n" + " '流向：' || t1.flow || ' 缺陷等级：' || " + " case t1.defect_level when '1' then '低' when '2' then '中' when '3' then '高' when '4' then '严重' else '无缺陷' end " + " || ' 管径：' || t1.pipe_diameter tip, " + "       t.shape.minx x,\n" + "       t.shape.miny y,\n" + " sde.st_astext(t.shape) wkt," + "       t.objectid\n" + "      from sde.JCGW t\n" + "       LEFT JOIN nkjc_defect T1 ON t.lno = t1.lno " + " left join PIPE_SECTION t2 on t1.pipe_no=t2.pipe_no" + " left join project_item t3 on t2.project_item_no=t3.item_no" + " left join PROJECT t4 on t3.project_no=t4.project_no) t " + "      WHERE 1=1 ";

		ParameterSqlModel parmSql = processParamSql(parms, sql);
		List<GlobalParameter> params = parmSql.getParms();
		sql = parmSql.getSql();

		if (parms.containsKey("keyWord")) {
			String keyWord = parms.get("keyWord");
			sql += " and (t.管线材料 like '%" + keyWord + "%' or t.管径或断面 like '%" + keyWord + "%' or t.排水 like '%" + keyWord + "%')";
		}

		String result = DataUtil.getPageSelectJson(sql, params);
		return result;
	}

	/**
	 * 获取管网设施数据
	 * 
	 * @param parms
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/getGWSS")
	@ResponseBody
	public String getGWSSInfo(String parmas) throws Exception {
		HashMap<String, String> parms = new HashMap();
		try {
			parms = getMapData(parmas);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String sql = "select t.行政区划 || t.街道名称 name, t.路名 address ,\n" + "       '类型：'||t.POINT_TYPE || ' 附属物：' ||t.附属物 || ' 路名：'|| t.路名 tip,\n" + "       t.shape.minx x,\n" + "       t.shape.miny y,\n" + "       t.OBJECTID,\n" + "       t.本点号,\n" + "       t.路名,\n" + " sde.st_astext(t.shape) wkt," + "       t.街道名称,\n" + "       t.行政区划,\n" + "       t.排水 管网类型\n" + "    from sde.PS_POINT t where 1=1 ";

		ParameterSqlModel parmSql = processParamSql(parms, sql);
		List<GlobalParameter> params = parmSql.getParms();
		sql = parmSql.getSql();

		if (parms.containsKey("keyWord")) {
			String keyWord = parms.get("keyWord");
			sql += " and (t.POINT_TYPE like '%" + keyWord + "%' or t.附属物  like '%" + keyWord + "%' or t.路名  like '%" + keyWord + "%')";
		}

		String result = DataUtil.getPageSelectJson(sql, params);
		return result;
	}

	/**
	 * 获取污水厂处理数据
	 * 
	 * @param parms
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/getWSC")
	@ResponseBody
	public String getWSCInfo(String parmas) throws Exception {
		HashMap<String, String> parms = new HashMap();
		try {
			parms = getMapData(parmas);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String sql = " select * from (select t1.name ,t.exp_no,\n" + "                      t1.shape.minx x,\n" + "                      t1.shape.miny y,\n" + " sde.st_astext(t1.shape) wkt," + "						t.contact_address address," + "                      t.img PHOTO,\n" + "                      t1.objectid,t.exp_no as wwtp_id,t.ATTENTION_LEVEL,t1.shape " + "                       from  BC_ENTERPRISE_ATTR t  " + "                      right join sde.ps_wwtpm t1 on t1.exp_no = t.exp_no) t " + "   where 1=1   ";

		ParameterSqlModel parmSql = processParamSql(parms, sql);
		List<GlobalParameter> params = parmSql.getParms();
		sql = parmSql.getSql();

		if (parms.containsKey("keyWord")) {
			String keyWord = parms.get("keyWord");
			sql += " and (t.address like '%" + keyWord + "%' )";
		}

		String result = DataUtil.getPageSelectJson(sql, params);
		JSONObject resultObject = JSONObject.fromObject(result);
		JSONArray newArray = new JSONArray();
		try {
			JSONArray array = resultObject.getJSONArray("data");
			for (int i = 0; i < array.size(); i++) {
				JSONObject jsonobject = array.getJSONObject(i);
				String expNO = jsonobject.getString("EXP_NO");
				List<RowBase> mnData = getMn(expNO);
				for (int j = 0; j < mnData.size(); j++) {
					RowBase r = mnData.get(j);
					String mn = r.getSafeString("DEVICE_NUM");
					String type = r.getSafeString("FLOW_TYPE");
					// 根据mn号获取状态
					String status = RedisHelper.getInstance().getStationStatus(mn);
					if (type.equals("0")) {
						jsonobject.put("inStatus", status);
					}
					if (type.equals("1")) {
						jsonobject.put("outStatus", status);
					}
				}
				newArray.add(jsonobject);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		JSONObject returnJsonObject = new JSONObject();
		returnJsonObject.put("data", newArray);
		returnJsonObject.put("pageInfo", resultObject.get("pageInfo"));
		returnJsonObject.put("paramInfo", resultObject.get("paramInfo"));
		return returnJsonObject.toString();
	}

	/**
	 * 查询排水管网详情
	 * 
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getDetailpsgw")
	@ResponseBody
	public String getDetailpsgw(String parmas) throws Exception {
		HashMap<String, String> parms = new HashMap();
		try {
			parms = getMapData(parmas);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		AndroidPipeDetail androiPipeDetail = new AndroidPipeDetail();
		JSONObject result = androiPipeDetail.getPipeDetail(parms);
		if (null != result) {
			return result.toString();
		} else {
			return null;
		}

	}

	/**
	 * 获取管网点详情数据
	 * 
	 * @param parms
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/getGWDDetail")
	@ResponseBody
	public String getDetailPipe(String parmas) throws Exception {
		HashMap<String, String> parms = new HashMap();
		try {
			parms = getMapData(parmas);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String stationCode = parms.get("stationCode");
		AndroidPipeDetail androiPipeDetail = new AndroidPipeDetail();
		String result = androiPipeDetail.getGWDDetail(stationCode);

		return result;
	}

	/**
	 * 获取规划管网详情数据
	 * 
	 * @param parms
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/getGHGWDetail")
	@ResponseBody
	public String getDetailGHGW(String parmas) throws Exception {
		HashMap<String, String> parms = new HashMap();
		try {
			parms = getMapData(parmas);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		AndroidPipeDetail androiPipeDetail = new AndroidPipeDetail();
		JSONObject result = androiPipeDetail.getGHGWDetail(parms);

		return result.toString();
	}

	/**
	 * 查询清源小区详情
	 * 
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getDetailqyxq")
	@ResponseBody
	public String getDetailqyxq(String parmas) throws Exception {
		HashMap<String, String> parms = new HashMap();
		try {
			parms = getMapData(parmas);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		AndroidPipeDetail androiPipeDetail = new AndroidPipeDetail();
		JSONObject result = androiPipeDetail.getqyxqDetail(parms);

		return result.toString();
	}

	/**
	 * 查询排水管网设施详情
	 * 
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getDetailpipeDevice")
	@ResponseBody
	public String getDetailpipeDevice(String parmas) throws Exception {
		HashMap<String, String> parms = new HashMap();
		try {
			parms = getMapData(parmas);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		AndroidPipeDetail androiPipeDetail = new AndroidPipeDetail();
		JSONObject result = androiPipeDetail.getpipeDeviceDetail(parms);

		return result.toString();
	}

	/**
	 * 查询内窥检测管网详情
	 * 
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getDetailjcgw")
	@ResponseBody
	public String getDetailjcgw(String parmas) throws Exception {
		HashMap<String, String> parms = new HashMap();
		try {
			parms = getMapData(parmas);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// AndroidPipeDetail androiPipeDetail = new AndroidPipeDetail();
		// TwoTuple<List<RowBase>, List<String>> result =
		// androiPipeDetail.getJCGWDetail(parms);
		// JSONArray attrJsonObject =
		// JsonUtil.ConvertArrayToJsonObject(result.first);
		// JSONArray attrNameJsobObject =
		// JsonUtil.ConvertArrayToJsonObject(result.second);
		// JSONObject resultObject = new JSONObject();
		// resultObject.put("attr", attrJsonObject);
		// resultObject.put("attrName", attrNameJsobObject);
		// String jsonResult = JsonUtil.ConvertObjectToJson(resultObject);

		return null;
	}

	/**
	 * 获取污水厂数采仪编码
	 * 
	 * @param wwt_id
	 * @return
	 */
	public List<RowBase> getMn(String wwt_id) {
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		String sql = "select t2.*,t1.device_num from BC_COLLECTION_INFO t1 left join  monitor_station t2 on t1.site_id=t2.exp_no where t2.wwtp_id='" + wwt_id + "'";
		List<RowBase> data = DataUtil.getSelectRows(sql, parms);
		return data;
	}

	private HashMap getMapData(String parmas) throws Exception {
		HashMap resultMap = new HashMap();
		parmas = parmas.replace("{", "");
		parmas = parmas.replace("}", "");
		parmas = parmas.replace(" ", "");
		String[] splite = parmas.split("\\,");
		for (int i = 0; i < splite.length; i++) {
			String[] item = splite[i].split("=");
			if (item.length > 1) {
				resultMap.put(item[0], item[1]);
			} else {
				resultMap.put(item[0], "");
			}

		}
		// TODO Auto-generated method stub
		return resultMap;
	}
}
